<?php

namespace Home\Controller;
use Common\Controller\BaseController;

/**
 * Class EquipmentController
 * @package Home\Controller
 * 设备管理
 */
class EquipmentController extends BaseController {

   public function index(){

        $equ_name = I('get.equ_name','','htmlspecialchars');
        $source = I('get.source','','htmlspecialchars');

        $equModel = D('Equipment');

        $where = " equipment.del=1 ";
        
        if(!empty($equ_name)){
        	$where  .= " and locate('{$equ_name}',equipment.name) ";
        }

        if(!empty($source)){
        	$where  .= " and locate('{$source}',equipment.source) ";
        }

        $field = "equipment.*,user.name as uname,user.nickname ";
        $list = $equModel
            ->field($field)
           ->join('user on equipment.user_id=user.user_id')
            ->where($where)
            ->order('reg_time DESC')
            ->select();
         // echo $equModel->_sql();
         // dump($list);exit;
        $this->assign('list',$list);
        $this->assign('equ_name',$equ_name);
        $this->assign('source',$source);

        $this->display('index');
    }

    public function add(){

        $this->display('add');
    }

    public function edit(){

        $equ_id    = I('get.equid');
        $equModel  = D('Equipment');

        $equipment = $equModel->where('equipment_id='.intval($equ_id).' and del=1')->limit(1)->select();

        if(empty($equipment)){
            $this->error('设备不存在');
        }

        
        $this->assign('equipment',$equipment[0]);
        $this->display('edit');
    }

    public function save(){

        $ret = array('status'=>200,'msg'=>'','content'=>'');

        if(IS_POST){
            try{

                $equModel = D('Equipment');
                $post      = I('post.');
                $res = $equModel->saveEquipment($post);
                $ret['status'] = (false==$ret) ? 0 : 200;
                $ret['msg'] = $res['msg'];

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '请求不允许';
        }

        $this->ajaxReturn($ret);
    }
    public function delete(){

        
        $equ_id    = I('get.equid');
        $equModel  = D('Equipment');

        $equipment = $equModel->where('equipment_id='.intval($equ_id).' and del=1')->limit(1)->select();
	
        if(!empty($equipment)){
            try{
                $info = array('equipment_id'=>intval($equ_id),'del'=>2);
                $res = $equModel->setField($info);
                if(false===$res){
                    $ret['status'] = 0;
                    $ret['msg']    = "删除失败";
                }else{
                    $ret['msg']    = "删除成功";
                }

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '设备不存在';
        }
        $this->ajaxReturn($ret);
    }

    public function view(){
        $equ_id    = I('get.equid');
        $equModel  = D('Equipment');
        
        $field = "equipment.*,user.name as uname,user.nickname";
        $equipment = $equModel
            ->field($field)
            ->join('user on equipment.user_id=user.user_id')
            ->where('equipment_id='.intval($equ_id).' and equipment.del=1')
            ->limit(1)
            ->select();

        if(empty($equipment)){
            $this->error('设备不存在');
        }

        $this->assign('equipment',$equipment[0]);
        $this->display('view');
    }
}